package main;

/**
 * @author zhx
 */

public class LeetCode14 {
    public String longestCommonPrefix(String[] strs) {
        StringBuilder sb = new StringBuilder();
        if (strs.length ==0){
            return sb.toString();
        }
        char tmp = 0;
        int j = 0;
        // 存在不相等的情况是为true
        boolean flag = false;

        do {
            for (int i = 0; i < strs.length; i++) {
                if (j < strs[0].length()) {
                    tmp = strs[0].charAt(j);
                } else {
                    flag = true;
                }
                if (j >= strs[i].length() || strs[i].charAt(j) != tmp) {
                    flag = true;
                }
            }
            if (flag) {
                break;
            } else {
                sb.append(tmp);
                j++;
            }
        } while (true);


        return sb.toString();
    }

}
